package com.zy.springcloud.alibaba.service.impl;

import com.zy.springcloud.alibaba.dao.OrderDao;
import com.zy.springcloud.alibaba.domain.Order;
import com.zy.springcloud.alibaba.service.AccountService;
import com.zy.springcloud.alibaba.service.OrderService;
import com.zy.springcloud.alibaba.service.StorageService;
import io.seata.spring.annotation.GlobalTransactional;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {

    @Resource
    private OrderDao orderDao;
    @Resource
    private StorageService storageService;
    @Resource
    private AccountService accountService;

    /**
     * 创建订单->调用库存服务减库存->调用账户服务减余额->修改订单状态 END
     * @param order
     */

    @Override
    @GlobalTransactional(name = "fsp_create-order",rollbackFor = Exception.class)
    public void create(Order order) {
//        1.新建订单
        log.info("------------>开始新建订单");
        orderDao.create(order);

//        2.扣减库存
        log.info("------------>订单微服务开始调用库存，做count扣减");
        storageService.decrease(order.getProductId(),order.getCount());
        log.info("------------>订单微服务开始调用库存，做扣减end");

//        3.扣减账户
        log.info("------------>订单微服务开始调用账户，做money扣减");
        accountService.decrease(order.getUserId(),order.getMoney());

//        4.修改订单状态，从零到一，1代表完成
        log.info("------------>修改订单状态开始");
        orderDao.update(order.getUserId(),0);
        log.info("------------>修改订单状态结束");

        log.info("------------>下单结束");


    }
}
